package ca.etsmtl.log430.lab3;

import java.io.PipedReader;
import java.io.PipedWriter;

public class RateFilter extends Thread {
	// Declarations

	boolean done;

	String comparator;
	int rate;
	PipedReader inputPipe = new PipedReader();
	PipedWriter outputPipe = new PipedWriter();

	public RateFilter(String comparator, int rate, PipedWriter inputPipe, PipedWriter outputPipe) {

		this.comparator = comparator;
		this.rate = rate;

		try {

			// Connect inputPipe
			this.inputPipe.connect(inputPipe);
			System.out.println("RateFilter " + comparator + " " + rate
					+ ":: connected to upstream filter.");

			// Connect outputPipe
			this.outputPipe = outputPipe;
			System.out.println("RateFilter " + comparator + " " + rate
					+ ":: connected to downstream filter.");

		} catch (Exception Error) {

			System.out.println("RateFilter " + comparator + " " + rate
					+ ":: error connecting to other filters.");

		} // try/catch

	} // Constructor

	// This is the method that is called when the thread is started
	public void run() {

		// Declarations

		char[] characterValue = new char[1];
		// char array is required to turn char into a string
		String lineOfText = "";
		// string is required to look for the keyword
		int integerCharacter; // the integer value read from the pipe

		try {

			done = false;

			while (!done) {

				integerCharacter = inputPipe.read();
				characterValue[0] = (char) integerCharacter;

				if (integerCharacter == -1) { // pipe is closed

					done = true;

				} else {

					if (integerCharacter == '\n') { // end of line

						System.out.println("RateFilter " + comparator + " " + rate
								+ ":: received: " + lineOfText + ".");
						
						int rate = Integer.valueOf(lineOfText.split(" ")[4]);
						if (comparator.equals("lessthan")) {
							if (rate < this.rate) {
								System.out.println("RateFilter " + comparator + " " + this.rate
										+ ":: sending: " + lineOfText + " to output pipe.");
								lineOfText += new String(characterValue);
								outputPipe.write(lineOfText, 0, lineOfText.length());
								outputPipe.flush();
							}
						}
						else if (comparator.equals("equal")) {
							if (rate == this.rate) {
								System.out.println("RateFilter " + comparator + " " + this.rate
										+ ":: sending: " + lineOfText + " to output pipe.");
								lineOfText += new String(characterValue);
								outputPipe.write(lineOfText, 0, lineOfText.length());
								outputPipe.flush();
							}
						}
						else if (comparator.equals("greaterthan")) {
							if (rate > this.rate) {
								System.out.println("RateFilter " + comparator + " " + this.rate
										+ ":: sending: " + lineOfText + " to output pipe.");
								lineOfText += new String(characterValue);
								outputPipe.write(lineOfText, 0, lineOfText.length());
								outputPipe.flush();
							}
						}
						else {
							System.out.println("RateFilter " + comparator + " " + rate
									+ ":: invalid comparator.");
							throw new Exception();
						}

						lineOfText = "";

					} else {

						lineOfText += new String(characterValue);

					} // if //

				} // if

			} // while

		} catch (Exception error) {

			System.out.println("RateFilter " + comparator + " " + rate + " ::interrupted.");

		} // try/catch

		try {

			inputPipe.close();
			System.out.println("RateFilter " + comparator + " " + rate
					+ ":: input pipe closed.");

			outputPipe.close();
			System.out.println("RateFilter " + comparator + " " + rate
					+ ":: output pipe closed.");

		} catch (Exception error) {

			System.out.println("RateFilter " + comparator + " " + rate
					+ ":: error closing pipes.");

		} // try/catch

	} // run
}
